const path = require('path')
const webpack = require('webpack')
const buildDate = JSON.stringify(new Date().toLocaleString())
const version = require('./package.json').version;
const CopyWebpackPlugin = require('copy-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const argv = process.env.npm_config_argv&&JSON.parse(process.env.npm_config_argv).original || process.argv;
function getEnvKey() {
  try {
    let argv = process.argv;
    if (process.env.npm_config_argv) {
      argv = JSON.parse(process.env.npm_config_argv).original;
    }
    let envKey = argv.slice(2).filter(item=>item.indexOf("env")>-1)[0].split("=")[1];
    return envKey;
  }catch(e){
    return null;
  }
}
function resolve (dir) {
  return path.join(__dirname, dir)
}
// console.log(process.env);
const isProd = process.env.NODE_ENV === 'production'

const assetsCDN = {
  // webpack build externals
  externals: {
    vue: 'Vue',
    'vue-router': 'VueRouter',
    vuex: 'Vuex',
    axios: 'axios'
  },
  css: [],
  // https://unpkg.com/browse/vue@2.6.10/
  js: [
    // '//cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js',
    // '//cdn.jsdelivr.net/npm/vue-router@3.1.3/dist/vue-router.min.js',
    // '//cdn.jsdelivr.net/npm/vuex@3.1.1/dist/vuex.min.js',
    // '//cdn.jsdelivr.net/npm/axios@0.19.0/dist/axios.min.js'
  ]
}
console.log(version);

// vue.config.js
const vueConfig = {
  publicPath:'./',
  assetsDir:'./public',
  pages: {
    index:{
      entry: 'src/main.js',
      template: './index.html',
      filename: 'index.html',
    }
  },
  configureWebpack: {
    output:{
      chunkFilename:isProd?'public/[contenthash].js':'public/[name].js'
    },
    // webpack plugins
    plugins: [
      // Ignore all locale files of moment.js
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
      new webpack.DefinePlugin({
        WEBAPP_VERSION: `"${version}"`,
        BUILD_DATE: buildDate,
        'process.env.key':`"${getEnvKey()}"`
      }),
      new CopyWebpackPlugin([
        {
          from: path.resolve(__dirname, './public'),
          to: './public',
          ignore: ['.*','*.scss']
        }
      ]),
      new MiniCssExtractPlugin({
        // ......
        ignoreOrder: true
      })
    ],
    // if prod, add externals
    // externals: isProd ? assetsCDN.externals : {}
  },

  chainWebpack: (config) => {
    config.resolve.alias
      .set('@$', resolve('src'))
      // .set('$', resolve('./'))

    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule
      .oneOf('inline')
      .resourceQuery(/inline/)
      .use('vue-svg-icon-loader')
      .loader('vue-svg-icon-loader')
      .end()
      .end()
      .oneOf('external')
      .use('file-loader')
      .loader('file-loader')
      .options({
        name: 'assets/[name].[hash:8].[ext]'
      })
      if (true) {
        config.output.chunkFilename = '[name].[contenthash].js';
        
      }
      // config.plugin('mini-css-extract-plugin')
      //   .use(require('mini-css-extract-plugin'), [{
      //         filename: '[name].[contenthash].css',
      //         chunkFilename: '[id].[contenthash].css'
      //     }]).end()
    // if prod is on
    // assets require on cdn
    // if (isProd) {
    //   config.plugin('html').tap(args => {
    //     args[0].cdn = assetsCDN
    //     return args
    //   })
    // }
  },

  css: {
    loaderOptions: {
      less: {
        modifyVars: {
          "primary-color": "#3D7F62", // 全局主色
          "link-color": "#287855", // 链接色
          // #F6F9FA
          "success-color": "#52c41a", // 成功色
          "warning-color": "#faad14", // 警告色
          "error-color": "#f5222d", // 错误色
          "font-size-base": "14px", // 主字号
          "heading-color": "rgba(0, 0, 0, 0.85)", // 标题色
          "text-color": "rgba(0, 0, 0, 0.65)", // 主文本色
          "text-color-secondary": "rgba(0, 0, 0, 0.45)", // 次文本色
          "disabled-color": "rgba(0, 0, 0, 0.25)", // 失效色
          "border-radius-base": "4px", // 组件/浮层圆角
          "border-color-base": "#d9d9d9", // 边框色
          "box-shadow-base": "0px 2px 4px 0px rgba(14,97,60,0.08)" // 浮层阴影
        },
        javascriptEnabled: true
      }
    }
  },

  devServer: {
    // development server port 8000
    host: "0.0.0.0",
    port: 8000,
    hotOnly: true, // 热更新
    // If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
    // proxy: {
    //   '/api': {
    //     target: '',
    //     ws: false,
    //     changeOrigin: true,
    //     pathRewrite: {
    //       ['^' + '/api']: ''
    //     }
    //   }
    // }
  },

  // disable source map in production
  productionSourceMap: false,
  lintOnSave: false,
  // babel-loader no-ignore node_modules/*
  transpileDependencies: []
}
// config.output.chunkFilename = '[name].[contenthash].js';
module.exports = vueConfig
